package com.sptus.web.quartz.job;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sptus.system.domain.*;
import com.sptus.system.service.*;
import com.sptus.web.util.DateTimeUtils;
import com.sptus.web.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
@Slf4j
public class sptDomeData {

    @Autowired
    IDevInfoMsg0Service devInfoMsg0Service;
    @Autowired
    IDevInfoMsg0HistoryService devInfoMsg0HistoryService;
    @Autowired
    IDevInfoMsg1Service devInfoMsg1Service;
    @Autowired
    IDevInfoMsg1HistoryService devInfoMsg1HistoryService;
    @Autowired
    IDevInfoMsg2Service devInfoMsg2Service;
    @Autowired
    IDevInfoMsg2HistoryService devInfoMsg2HistoryService;
    @Autowired
    IDevInfoMsg3Service devInfoMsg3Service;
    @Autowired
    IDevInfoMsg3HistoryService devInfoMsg3HistoryService;
    @Autowired
    IDevInfoMsg4Service devInfoMsg4Service;
    @Autowired
    IDevInfoMsg4HistoryService devInfoMsg4HistoryService;
    @Autowired
    IDevInfoMsg5Service devInfoMsg5Service;
    @Autowired
    IDevInfoMsg6Service devInfoMsg6Service;

    //数据模拟
    @Scheduled(cron = "0 0/10 * * * ?")
    public void run (){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        String format = s.format(new Date());
        String original = simpleDateFormat.format(new Date());
        String newDate = sptDomeData.getDate( new Date().getDay());
        String end = DateTimeUtils.replaceDate(original, newDate);
        String start = TimeUtils.getTenMinutesBefore(end);
        //获取实时数据


        LambdaQueryWrapper<DevInfoMsg0History> devInfoMsg0LambdaQueryWrapper = new LambdaQueryWrapper<>();
        devInfoMsg0LambdaQueryWrapper.between(DevInfoMsg0History::getCreateTime,start,end);
        devInfoMsg0LambdaQueryWrapper.eq(DevInfoMsg0History::getSn,"96142402600053");
        List<DevInfoMsg0History> devInfoMsg0Histories = devInfoMsg0HistoryService.getBaseMapper().selectList(devInfoMsg0LambdaQueryWrapper);
        System.out.println(devInfoMsg0Histories);

        for (DevInfoMsg0History devInfoMsg0History : devInfoMsg0Histories) {
            devInfoMsg0History.setSn("96142403105799");
            devInfoMsg0History.setId(null);
            devInfoMsg0History.setCreateTime(DateTimeUtils.replaceDate(devInfoMsg0History.getCreateTime(), format));
        }
        if(devInfoMsg0Histories.size()>0){
            String jsonString = JSONObject.toJSONString(devInfoMsg0Histories.get(devInfoMsg0Histories.size() - 1));
            DevInfoMsg0 devInfoMsg0 = JSONObject.parseObject(jsonString, DevInfoMsg0.class);
            LambdaQueryWrapper<DevInfoMsg0> devInfoMsg0LambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            devInfoMsg0LambdaQueryWrapper1.eq(DevInfoMsg0::getSn,"96142403105799");
            devInfoMsg0Service.update(devInfoMsg0,devInfoMsg0LambdaQueryWrapper1);
        }

        devInfoMsg0HistoryService.saveBatch(devInfoMsg0Histories);

        //msg1
        LambdaQueryWrapper<DevInfoMsg1History> devInfoMsg1LambdaQueryWrapper = new LambdaQueryWrapper<>();
        devInfoMsg1LambdaQueryWrapper.between(DevInfoMsg1History::getCreateTime,start,end);
        devInfoMsg1LambdaQueryWrapper.eq(DevInfoMsg1History::getSn,"96142402600053");
        List<DevInfoMsg1History> devInfoMsg1Histories = devInfoMsg1HistoryService.getBaseMapper().selectList(devInfoMsg1LambdaQueryWrapper);
        for (DevInfoMsg1History devInfoMsg1History : devInfoMsg1Histories) {
            devInfoMsg1History.setSn("96142403105799");
            devInfoMsg1History.setId(null);
            devInfoMsg1History.setCreateTime(DateTimeUtils.replaceDate(devInfoMsg1History.getCreateTime(), format));
        }
        devInfoMsg1HistoryService.saveBatch(devInfoMsg1Histories);
        System.out.println(devInfoMsg1Histories);
        //msg2
        LambdaQueryWrapper<DevInfoMsg2History> devInfoMsg2LambdaQueryWrapper = new LambdaQueryWrapper<>();
        devInfoMsg2LambdaQueryWrapper.between(DevInfoMsg2History::getCreateTime,start,end);
        devInfoMsg2LambdaQueryWrapper.eq(DevInfoMsg2History::getSn,"96142402600053");
        List<DevInfoMsg2History> devInfoMsg2Histories = devInfoMsg2HistoryService.getBaseMapper().selectList(devInfoMsg2LambdaQueryWrapper);
        for (DevInfoMsg2History devInfoMsg2History : devInfoMsg2Histories) {
            devInfoMsg2History.setSn("96142403105799");
            devInfoMsg2History.setId(null);
            devInfoMsg2History.setCreateTime(DateTimeUtils.replaceDate(devInfoMsg2History.getCreateTime(), format));
        }
        devInfoMsg2HistoryService.saveBatch(devInfoMsg2Histories);
        System.out.println(devInfoMsg2Histories);
        LambdaQueryWrapper<DevInfoMsg3History> devInfoMsg3LambdaQueryWrapper = new LambdaQueryWrapper<>();
        devInfoMsg3LambdaQueryWrapper.between(DevInfoMsg3History::getCreateTime,start,end);
        devInfoMsg3LambdaQueryWrapper.eq(DevInfoMsg3History::getSn,"96142402600053");
        List<DevInfoMsg3History> devInfoMsg3Histories = devInfoMsg3HistoryService.getBaseMapper().selectList(devInfoMsg3LambdaQueryWrapper);
        for (DevInfoMsg3History devInfoMsg3History : devInfoMsg3Histories) {
            devInfoMsg3History.setSn("96142403105799");
            devInfoMsg3History.setId(null);
            devInfoMsg3History.setCreateTime(DateTimeUtils.replaceDate(devInfoMsg3History.getCreateTime(), format));
        }
        devInfoMsg3HistoryService.saveBatch(devInfoMsg3Histories);
        System.out.println(devInfoMsg3Histories);

        LambdaQueryWrapper<DevInfoMsg4History> devInfoMsg4LambdaQueryWrapper = new LambdaQueryWrapper<>();
        devInfoMsg4LambdaQueryWrapper.between(DevInfoMsg4History::getCreateTime,start,end);
        devInfoMsg4LambdaQueryWrapper.eq(DevInfoMsg4History::getSn,"96142402600053");
        List<DevInfoMsg4History> devInfoMsg4Histories = devInfoMsg4HistoryService.getBaseMapper().selectList(devInfoMsg4LambdaQueryWrapper);
        for (DevInfoMsg4History devInfoMsg4History : devInfoMsg4Histories) {
            devInfoMsg4History.setSn("96142403105799");
            devInfoMsg4History.setId(null);
            devInfoMsg4History.setCreateTime(DateTimeUtils.replaceDate(devInfoMsg4History.getCreateTime(), format));
        }
        devInfoMsg4HistoryService.saveBatch(devInfoMsg4Histories);
        System.out.println(devInfoMsg3Histories);

    }

    public void execute() throws JobExecutionException, ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        String format = s.format(new Date());
        String original = simpleDateFormat.format(new Date());
        String newDate = sptDomeData.getDate( new Date().getDay());
        String end = DateTimeUtils.replaceDate(original, newDate);
        String start = TimeUtils.getTenMinutesBefore(end);
        LambdaQueryWrapper<DevInfoMsg0History> devInfoMsg0LambdaQueryWrapper = new LambdaQueryWrapper<>();
        devInfoMsg0LambdaQueryWrapper.between(DevInfoMsg0History::getCreateTime, "2024-11-24","2024-11-25");
        devInfoMsg0LambdaQueryWrapper.eq(DevInfoMsg0History::getSn,"96142402600053");
        List<DevInfoMsg0History> devInfoMsg0Histories = devInfoMsg0HistoryService.getBaseMapper().selectList(devInfoMsg0LambdaQueryWrapper);
        for (DevInfoMsg0History devInfoMsg0History : devInfoMsg0Histories) {
            devInfoMsg0History.setSn("96142403105799");
            devInfoMsg0History.setId(null);
            devInfoMsg0History.setCreateTime(DateTimeUtils.replaceDate(devInfoMsg0History.getCreateTime(), sptDomeData.getDate1(s.parse(devInfoMsg0History.getCreateTime()).getDay())));
        }
        devInfoMsg0HistoryService.saveBatch(devInfoMsg0Histories);
//        System.out.println(devInfoMsg0Histories);
    }

    public static String  getDate(int day){
        String date ="2024-11-18";
        if(0==day){
            date="2024-11-24";
        }else if(1==day){
            date ="2024-11-18";
        }else if(2==day){
            date ="2024-11-19";
        }else if(3==day){
            date ="2024-11-20";
        }else if(4==day){
            date ="2024-11-21";
        }else if(5==day){
            date ="2024-11-22";
        }else if(6==day){
            date ="2024-11-23";
        }
        return date;
    }
    public static String  getDate1(int day){
        String  date ="2025-03-10";
        if(0==day){
            date="2025-03-16";
        }else if(1==day){
            date ="2025-03-10";
        }else if(2==day){
            date ="2025-03-11";
        }else if(3==day){
            date ="2025-03-12";
        }else if(4==day){
            date ="2025-03-13";
        }else if(5==day){
            date ="2025-03-14";
        }else if(6==day){
            date ="2025-03-15";
        }
        return date;
    }
}
